package weather;

import java.awt.*;
import java.util.Calendar;

import javax.swing.*;

import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.chart.*;
import org.jfree.chart.plot.PlotOrientation;

public class WeatherGrapher extends JPanel{
	private String getHour(int i){
		String[] hour = {"12AM","1AM","2AM","3AM","4AM","5AM","6AM","7AM","8AM","9AM","10AM","11AM","12PM","1PM","2PM","3PM","4PM","5PM","6PM","7PM","8PM","9PM","10PM","11PM"};
		return hour[i];
		}
	private String getWindMinute(int i){
		String[] windMinute={"0","5","10","15","20","25","30","35","40","45","50","55","60"};
		return windMinute[i];
	}
        private double [] getTemps() throws Exception{
                DatabaseHelper helper = new DatabaseHelper();
                double[] temps = new double[24];
                int i=0;
               
                try{
                        while(i<24)
                        {
                                temps[i]=helper.dbGetTemp(i+1);
                                i++;
                        }
                }
                catch(Exception e){
                        throw e;
                        }
                return temps;
        }
        private double [] getWinds() throws Exception{
                DatabaseHelper helper = new DatabaseHelper();
                double[] winds = new double[14];
                int i=0;
               
                try{
                        while(i<14)
                        {
                                winds[i]=helper.dbGetWind(i+1);
                                i++;
                        }
                }
                catch(Exception e){
                        throw e;
                        }
                return winds;
        }
       
        
        
        private JFreeChart createTempChart(final CategoryDataset dataset){
               
                //create chart
                final JFreeChart chart = ChartFactory.createLineChart(
                                "Temperature Over Last 24 Hours",       //Title
                                "Hour",                                                         //X Axis Label
                                "Temperature",                                          //Y Axis Label
                                dataset,
                                PlotOrientation.VERTICAL,
                                true,
                                false,
                                false
                );
               
                //add customization here
                return chart;
        }
       
        private JFreeChart createWindChart(final CategoryDataset dataset){
               
                //create chart
                final JFreeChart chart = ChartFactory.createLineChart(
                                "Wind Speed Over Last Minute",        //Title
                                "Seconds Ago",                                                         //X Axis Label
                                "Wind",  				                                        //Y Axis Label
                                dataset,
                                PlotOrientation.VERTICAL,
                                true,
                                false,
                                false
                );
               
                //add customization here
                return chart;
        }
       
        public JPanel tempGraph() throws Exception{
                WeatherGrapher wg= new WeatherGrapher();
                int i=0,hour=0;
                double [] temps = new double[24];
                Calendar cal=Calendar.getInstance();
               
                //JFreeChart objects
                //final XYSeriesCollection tempSet = new XYSeriesCollection();
                final String series = "Temperature";
                final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
                //retrieve temperature data
                while(i<24){
                        temps=wg.getTemps();
                        i++;
                }
               
                i=0;
                //set up series (Adjust later to start at current hour)
                String col;
                /*
                while(i<24)
                {
                        series.add(i+1,temps[i] );
                        i++;
                }
                */
                //tempSet.addSeries(series);
                
        		hour=cal.get(Calendar.HOUR_OF_DAY);
        		//Adjust graph to current time
        		while(i<24)
        		{
        			if(hour>23)
        			{
        				dataset.addValue(temps[hour-24], series, wg.getHour(hour-24));
        			}
        			else
        			{
        				if(hour==23)
        				{
        					dataset.addValue(temps[0], series, wg.getHour(0));
        				}
        				else
        				{
        					dataset.addValue(temps[hour++], series, wg.getHour(hour++));
        				}
        			}
        			i++;
        			hour++;
        		}
               
                //Create Chart
                final JFreeChart chart = createTempChart(dataset);
                final ChartPanel cPanel = new ChartPanel(chart);
                cPanel.setPreferredSize(new Dimension(500,600));
                return cPanel;
                //setContentPane(cPanel);
        }
        
        public JPanel windGraph() throws Exception{
                WeatherGrapher wg= new WeatherGrapher();
                int i=0,hour=0;
                Calendar cal=Calendar.getInstance();
                double [] winds = new double[14];
               
                //JFreeChart objects
                //final XYSeriesCollection tempSet = new XYSeriesCollection();
                final String series = "Wind";
                final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
                //retrieve temperature data
                winds=wg.getWinds();
               
                //i=0;
                //set up series (Adjust later to start at current hour)
                String col;
                /*
                while(i<24)
                {
                        series.add(i+1,winds[i] );
                        i++;
                }
                tempSet.addSeries(series);
               */
                //hour=cal.get(Calendar.HOUR_OF_DAY);
        		//Adjust graph to current time
        		/*while(i<24)
        		{
        			if(hour>23)
        			{
        				dataset.addValue(winds[hour-24], series, wg.getHour(hour-24));
        			}
        			else
        			{
        				if(hour==23)
        				{
        					dataset.addValue(winds[0], series, wg.getHour(0));
        				}
        				else
        				{
        					dataset.addValue(winds[hour++], series, wg.getHour(hour++));
        				}
        			}
        			i++;
        			hour++;
        		}*/
                i=12;
                int i2=0;
                while(i2<13)
                {
                	dataset.addValue(winds[i],series,wg.getWindMinute(i));
                	i--;
                	i2++;
                }
                //Create Chart
                final JFreeChart chart = createWindChart(dataset);
                final ChartPanel cPanel = new ChartPanel(chart);
                cPanel.setPreferredSize(new Dimension(500,600));
                return cPanel;
                //setContentPane(cPanel);
        }
        
        public static void main (String args[]) throws Exception{
                /*WeatherGrapher wg = new WeatherGrapher();
                JFrame frame = new JFrame("WeatherGrapher");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
                frame.setBounds(0, 0, screenSize.width,screenSize.height);
                frame.setVisible(true);
                */
               
                //final WeatherGrapher wg = new WeatherGrapher();
                //wg.tempGraph();
                //wg.windGraph();
               // wg.setVisible(true);
        }
}

